home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / HDX_BACK / HDX350 / EPART.BAK < prev    next >
Encoding:
Text File  |  2001-02-09  |  12.9 KB  |  545 lines

  1.  
  2. /* epart.c */
  3.  
  4. /*
  5.  * Partition edit and pick for MS-DOS
  6.  *
  7.  * 08-Oct-1988    jye.  change and add this codes to do the partition 
  8.  *                       editing and picking for MS-DOS
  9.  * 18-NOV-1988  jye.  change and add codes in the partition editing and
  10.  *                      picking for the user interface.
  11.  */
  12.  
  13. #include "obdefs.h"
  14. #include "gemdefs.h"
  15. #include "osbind.h"
  16. #include "mydefs.h"
  17. #include "part.h"
  18. #include "ipart.h"
  19. #include "bsl.h"
  20. #include "hdx.h"
  21. #include "addr.h"
  22. #include "error.h"
  23.  
  24. #define SCR 1            /* use for extended partition box */
  25. #define PRM 0            /* use for primary partition box */
  26. #define ROLL1 1            /* move the bar one step */
  27. #define ROLL4 4            /* move the bar four steps */
  28.  
  29. extern long ostack;
  30. extern int wdesk;
  31. extern int hdesk;
  32. extern int ibm2st;
  33. extern int toibm;
  34. extern long bslsiz;
  35. extern SECTOR badbuf[];        /* bad sectors buffer */
  36. extern long gbslsiz();
  37. extern int npart;                /* number of partitions */
  38. extern int ext;                /* the index of extended partition */
  39. extern int uplim;                /* index of dialog box */
  40. extern int typedev;
  41. extern int typedrv;
  42. extern int firstmsg;            /* 1: 1st time to show the max partition */
  43.                                 /* alert box */
  44.  
  45. /*
  46.  * Global variables these routines communicate with
  47.  *
  48.  */
  49. static PART *pinfo;        /* -> partition block */
  50. static int totcyl;        /* total # of cylinder */
  51. static long sizleft;    /* size of unused disk in blocks */
  52. static long extleft;    /* size of unused extended partition in blocks */
  53. static int formw, formh;
  54. static int lx, ly, sx, sy;
  55. static int ok2draw;        /* 0: don't draw PARTPNL boxes */
  56. static char partnames[NAMSIZ];    /* partition name buffer */
  57. static int menuflg;        /* negative: never called partmenu */
  58. static int pnlflg;        /* 1: partition scheme comes from panel */
  59. static int along;        /* 1: will not redraw and clean the box */
  60. static int lowlim;        /* index of dialog box */
  61. static int first;        /* flag for add bad and good sectors only one time */
  62. long disksiz;            /* size of disk in blocks */
  63. long sumsiz;            /* the sum of bytes of root sectors */
  64. long spcyl;                /* sector per cylinder */
  65. long sptrk;                /* sector per track */
  66. int ibmpart;            /* IBM or ST partition flag. 0 ST partition else IBM */
  67. int yesscan;            /* the flag for the func. IBMGPART use */
  68. int tolpart;            /* the total of number partitions */
  69. int epty;                /* the y-coordinate of the moving bar */
  70. int restept;            /* 1: rest the moving bar to initial place */
  71.  
  72.  
  73.  
  74. #define MFM 17            /* sectors per track of MFM */
  75. #define RLL 26            /* sectors per track of RLL */
  76.  
  77. /* 
  78.  *     get IBM disk size.
  79.  */
  80.  
  81. ibmgdsiz(dev, bs)
  82.  
  83. int dev;
  84. char *bs;
  85.  
  86. {
  87.     char *num;
  88.     BYTE spt,numhead;
  89.     int i, ret, numtrack;
  90.     extern long get3bytes();
  91.     SETMODE *mb;
  92.     SECTOR size, msiz;    /* size of media */
  93.     char buf[512], sendata[32];
  94.     long dmaptr, tmpptr;
  95.     char *dmahigh=0xffff8609,
  96.          *dmamid=0xffff860b,
  97.          *dmalow=0xffff860d;
  98.  
  99.     ostack = Super(NULL);
  100.     /* get format parameters/ disk size from emdia */
  101.     ret = mdsense(dev, 0, 0, 16, sendata);
  102.     delay();
  103.     Super(ostack);
  104.     if (ret != 0)     {
  105.         return ERROR;
  106.     }
  107.  
  108.     /* check what kind of sense data it returned  */
  109.     /* If full SCSI, will return number of blocks */
  110.     /* on disk at byte 5, 6 and 7.  If Adaptec,   */
  111.     /* will return 0 for number of blocks on disk */
  112.     /* on SCSI. */
  113.    
  114.        if (!(msiz = get3bytes(sendata+5))) {    /* no disk size returned??? */
  115.            /* it's adaptec's.  Interpret as SETMODE structure */
  116.            mb = (SETMODE *)sendata;
  117.         /* get number of cylinders */
  118.         totcyl = mb->smd_cc[0];
  119.         totcyl <<= 8;
  120.         totcyl |= mb->smd_cc[1];
  121.  
  122.         /* get number of heads */
  123.         numhead = mb->smd_dhc;
  124.    
  125.         sptrk = (long)MFM;
  126.         msiz = (SECTOR)numhead * (SECTOR)totcyl * sptrk;
  127.         spcyl = (SECTOR)numhead * sptrk;
  128.    
  129.         for (i = 0; i < 20; i++) {
  130.             if ((ret = rdsects(dev, 1, buf, msiz+i)) == OK) {
  131.  
  132.             /* find out whether data has been transferred, by
  133.                   checking if dma pointer has been moved.      */
  134.  
  135.                    ostack = Super(NULL);    /* get into Supervisor mode */
  136.        
  137.                 dmaptr = *dmahigh;
  138.                 dmaptr &= 0x0000003f;
  139.                 dmaptr <<= 16;
  140.                 tmpptr = *dmamid;
  141.                 tmpptr &= 0x000000ff;
  142.                 tmpptr <<= 8;
  143.                 dmaptr |= tmpptr;
  144.                 tmpptr = *dmalow;
  145.                 tmpptr &= 0x000000ff;
  146.                 dmaptr |= tmpptr;
  147.  
  148.                 delay();
  149.                 Super(ostack);        /* back to user mode */
  150.  
  151.                 if (dmaptr != buf)
  152.                     break;
  153.             } else {            /* rdsects return an error */
  154.                 if (tsterr(ret) == OK) {
  155.                         break;
  156.                 }
  157.                }
  158.             }
  159.  
  160.         if (ret == MDMCHGD)        /* check if error occurred */
  161.             return err(cruptfmt);
  162.  
  163.         /* Determine if media is MFM or RLL */
  164.         if (i < 20)        {
  165.             sptrk = (long)RLL;
  166.             msiz = (SECTOR)numhead * (SECTOR)totcyl * sptrk;
  167.             spcyl = (SECTOR)numhead * sptrk;
  168.         }
  169.        }
  170.     if ((ibmpart) || (!ibm2st))        {
  171.         numhead = IBMHEAD;
  172.         sptrk = IBMSPT;
  173.          totcyl = (int)(msiz / (sptrk * numhead));
  174.         if (totcyl > IBMMCYL)    {
  175.             numhead = (BYTE)(((msiz -1) / (sptrk * 1024)) + 2);
  176.         }
  177.         /* the number of sector in one cylinder.*/ 
  178.         /*    'spcyl' is a global value */
  179.         spcyl = sptrk * (long)numhead;
  180.     }
  181.     /* assign the size of disk */
  182.     disksiz = msiz;
  183. }
  184.  
  185.  
  186. /* 
  187.  * get disk size for ST
  188.  */
  189.  
  190. getdsiz(dev)
  191. int dev;
  192. {
  193.     char *num;
  194.     BYTE sptrk,numhead;
  195.     int i, ret, totcyl, numtrack, set, scsidrv, mask=0x0001;
  196.     extern long get3bytes();
  197.     SETMODE *mb;
  198.     SECTOR size, msiz;    /* size of media */
  199.     char buf[512], sendata[32];
  200.     long dmaptr, tmpptr, spcyl;
  201.     char *dmahigh=0xffff8609,
  202.          *dmamid=0xffff860b,
  203.          *dmalow=0xffff860d;
  204.  
  205.     ostack = Super(NULL);
  206.     /* get format parameters/ disk size from emdia */
  207.     set = typedev & (mask << dev);
  208.     scsidrv = typedrv & (mask << dev);
  209.     if ((set) || (scsidrv))        {
  210.         ret = mdsense(dev, 4, 0, 16, sendata);
  211.     } else    {
  212.         ret = mdsense(dev, 0, 0, 22, sendata);
  213.     }
  214.     delay();
  215.     Super(ostack);
  216.     if (ret != 0)     {
  217.         return ERROR;
  218.     }
  219.  
  220.     /* check what kind of sense data it returned  */
  221.     /* If full SCSI, will return number of blocks */
  222.     /* on disk at byte 5, 6 and 7.  If Adaptec,   */
  223.     /* will return 0 for number of blocks on disk */
  224.     /* on SCSI. */
  225.    
  226.        if (!(msiz = get3bytes(sendata+5))) {    /* no disk size returned??? */
  227.            /* it's adaptec's.  Interpret as SETMODE structure */
  228.            mb = (SETMODE *)sendata;
  229.         /* get number of cylinders */
  230.         totcyl = mb->smd_cc[0];
  231.         totcyl <<= 8;
  232.         totcyl |= mb->smd_cc[1];
  233.  
  234.         /* get number of heads */
  235.         numhead = mb->smd_dhc;
  236.    
  237.         sptrk = (long)MFM;
  238.         msiz = (SECTOR)numhead * (SECTOR)totcyl * sptrk;
  239.    
  240.         for (i = 0; i < 20; i++) {
  241.             if ((ret = rdsects(dev, 1, buf, msiz+i)) == OK) {
  242.  
  243.             /* find out whether data has been transferred, by
  244.                   checking if dma pointer has been moved.      */
  245.  
  246.                    ostack = Super(NULL);    /* get into Supervisor mode */
  247.        
  248.                 dmaptr = *dmahigh;
  249.                 dmaptr &= 0x0000003f;
  250.                 dmaptr <<= 16;
  251.                 tmpptr = *dmamid;
  252.                 tmpptr &= 0x000000ff;
  253.                 tmpptr <<= 8;
  254.                 dmaptr |= tmpptr;
  255.                 tmpptr = *dmalow;
  256.                 tmpptr &= 0x000000ff;
  257.                 dmaptr |= tmpptr;
  258.  
  259.                 delay();
  260.                 Super(ostack);        /* back to user mode */
  261.  
  262.                 if (dmaptr != buf)
  263.                     break;
  264.             } else {            /* rdsects return an error */
  265.                 if (tsterr(ret) == OK) {
  266.                         break;
  267.                 }
  268.                }
  269.             }
  270.  
  271.         if (ret == MDMCHGD)        /* check if error occurred */
  272.             return err(cruptfmt);
  273.  
  274.         /* Determine if media is MFM or RLL */
  275.         if (i < 20)        {
  276.             msiz = (SECTOR)numhead * (SECTOR)totcyl * (long)RLL;
  277.         }
  278.        }
  279.     return(msiz);
  280. }
  281.  
  282.  
  283.  
  284. /* 
  285.  * get ST partition information for ST structure
  286.  */
  287.  
  288. stgpart(xdev, bs, rpinfo)
  289.  
  290. int xdev;
  291. char *bs;
  292. PART **rpinfo;
  293.  
  294. {
  295.     int i, j, linkst, ret;
  296.     long sect;
  297.     register PART *ipart;
  298.     PART *xpinfo;
  299.  
  300.     if ((npart = countpart(xdev)) == ERROR)    {
  301.         return ERROR;
  302.     }
  303.     if ((*rpinfo = (PART *)Malloc((long)sizeof(PART)*npart)) <= 0)    {
  304.         err(nomemory);
  305.         if (rpinfo > 0)        Mfree(rpinfo);
  306.         return ERROR;
  307.     }
  308.     xpinfo = *rpinfo;
  309.     inipart(xpinfo, npart);
  310.        ipart = &((RSECT *)(bs + 0x200 - sizeof(RSECT)))->hd_p[0];
  311.     for (i = 0; i < NPARTS; i++, ipart++)    {
  312.         xpinfo[i].p_flg = ipart->p_flg;
  313.         xpinfo[i].p_st = ipart->p_st;
  314.         xpinfo[i].p_siz = ipart->p_siz;
  315.         if (ipart->p_flg & P_EXISTS)    {
  316.             if (i == ext)        {
  317.                 xpinfo[i].p_id[0] = 'X';
  318.                 xpinfo[i].p_id[1]  ='G';
  319.                 xpinfo[i].p_id[2] = 'M';
  320.             } else if (xpinfo[i].p_siz < MB16)    {
  321.                 xpinfo[i].p_id[0] = 'G';
  322.                 xpinfo[i].p_id[1]  ='E';
  323.                 xpinfo[i].p_id[2] = 'M';
  324.             } else {
  325.                 xpinfo[i].p_id[0] = 'B';
  326.                 xpinfo[i].p_id[1]  ='G';
  327.                 xpinfo[i].p_id[2] = 'M';
  328.             }
  329.         }
  330.     }
  331.     if (ext != NO_EXT)    { /* there are extended partition */
  332.         sect = xpinfo[ext].p_st;
  333.         do    {
  334.             if ((ret = getroot(xdev, bs, sect)) != 0) {
  335.                     if (tsterr(ret) != OK)
  336.                     err(rootread);
  337.                 return ERROR;
  338.             }
  339.                ipart = &((RSECT *)(bs + 0x200 - sizeof(RSECT)))->hd_p[0];
  340.             xpinfo[i].p_flg = ipart->p_flg;
  341.             xpinfo[i].p_st = sect;
  342.             xpinfo[i].p_siz = ipart->p_siz + ROOTSECT;
  343.             if (xpinfo[i].p_siz < MB16)    {
  344.                 xpinfo[i].p_id[0] = 'G';
  345.                 xpinfo[i].p_id[1]  ='E';
  346.                 xpinfo[i].p_id[2] = 'M';
  347.             } else {
  348.                 xpinfo[i].p_id[0] = 'B';
  349.                 xpinfo[i].p_id[1]  ='G';
  350.                 xpinfo[i].p_id[2] = 'M';
  351.             }
  352.             ipart++;                
  353.             i++;
  354.             sect = ipart->p_st + xpinfo[ext].p_st;
  355.         } while ((ipart->p_id[0] == 'X') && (ipart->p_id[1] == 'G') &&
  356.                 (ipart->p_id[2] == 'M'));  /* more partition */
  357.     } 
  358.     return OK;
  359. }
  360.  
  361.  
  362.  
  363. maxmsg(pnl,num,tolpart)
  364.  
  365. OBJECT *pnl;
  366. int num, tolpart;
  367.  
  368. {
  369.         int ret;     /* NOMAX: not to partition it. Else yes. */
  370.  
  371.         if ((num == MAXLOGDEVS) && (tolpart > MAXLOGDEVS - 1))    {
  372.             ret = form_alert(2, maxlogms);
  373.             return ((ret == NOMAX) ? (NOMAX) : (YES));
  374.         }
  375. }
  376.  
  377.  
  378. /*
  379.  * find out the total partitions
  380.  */
  381.  
  382. totalpart(num, str)
  383.  
  384. int num;
  385. char *str;
  386.  
  387. {
  388.     int  i=0, j=0;
  389.     char tem[10];
  390.  
  391.     /* conver the integer to ASCII */
  392.     do    {    /* generate digits in reverse order */
  393.         tem[i++] = num % 10 + '0';    /* get next digits */
  394.     } while ((num /= 10) > 0);        /* delete it */
  395.  
  396.     for (; i > 0; )    { /* reverse string 'str' in place */
  397.         *str++ = tem[--i];
  398.         j++;
  399.     }
  400.     for (; j < 5; j++)    
  401.         *str++ = ' ';
  402.        *str = '\0';
  403. }
  404.  
  405.  
  406.  
  407.  
  408. /*
  409.  * Partition button number-to-object translation table.
  410.  */
  411. int ppart[] = {
  412.     PPART0, PPART1, PPART2, PPART3,
  413.     PPART4, PPART5, PPART6, PPART7,
  414.     PPART8, PPART9, PPART10, PPART11,
  415.     PPART12, PPART13, PPART14
  416. };
  417.  
  418.  
  419. /*
  420.  * Check if partition scheme selected is _legal_.
  421.  * _Legal_ means the scheme does not map to non-existing memory.
  422.  * If it is not, prompt user to pick partition again.
  423.  *    Input:
  424.  *        pdev - physical unit we're trying to partition.
  425.  *        xpinfo - partition block user selected.
  426.  *    Return:
  427.  *        OK - if partition scheme is _legal_.
  428.  *        ERROR - if partition scheme is illegal.
  429.  */
  430.  
  431. ichkpart(pdev, xpinfo)
  432. int pdev;
  433. PART *xpinfo;
  434. {
  435.     char bs[512];
  436.     SECTOR totsiz;
  437.     int     i, ret;
  438.     
  439.     /*
  440.      * Get partition information from disk's root block.
  441.      */
  442.     if (!ibmpart)    {    /* initailly ST partition */
  443.         if ((ret = getroot(pdev, bs, (SECTOR)0)) != 0) {
  444.                 if (tsterr(ret) != OK)
  445.                 err(rootread);
  446.             return ERROR;
  447.         }
  448.         disksiz = ((RSECT *)(bs + 0x200 - sizeof(RSECT)))->hd_siz;
  449.     } 
  450.     totsiz = 0;
  451.     for (i = 0; i < NPARTS; i++) {
  452.         totsiz += xpinfo[i].p_siz;
  453.         if (totsiz > disksiz)
  454.             return ERROR;
  455.     }
  456.     return OK;
  457. }
  458.     
  459.  
  460.  
  461. inipart(xpart, npart)
  462. PART *xpart;
  463. int npart;
  464. {
  465.     int i;
  466.  
  467.     for (i = 0; i < npart; i++)    {
  468.         xpart[i].p_siz = 0L;
  469.         xpart[i].p_flg = 0;
  470.         xpart[i].p_st = 0L;
  471.         xpart[i].p_id[0] = 0;
  472.         xpart[i].p_id[1] = 0;
  473.         xpart[i].p_id[2] = 0;
  474.     }
  475. }
  476.  
  477.  
  478.  
  479.  
  480. kindST(dev)
  481. int dev;
  482. {
  483.     char bs[512];
  484.     PART *ipart;
  485.     int ret; 
  486.  
  487.     if ((ret = getroot(dev, bs, (SECTOR)0)) != 0)    {
  488.         if (tsterr(ret) != OK)
  489.             err(rootread);
  490.         return ERROR;
  491.     }
  492.     ipart = &((RSECT *)(bs + 0x200 - sizeof(RSECT)))->hd_p[0];
  493.     if (((ipart+1)->p_flg & P_EXISTS) && 
  494.         ((ipart+1)->p_id[0] == 'X') && 
  495.         ((ipart+1)->p_id[1] == 'G') && 
  496.         ((ipart+1)->p_id[2] == 'M'))    {
  497.         return (1);                /* link list ST partition */
  498.     } else {    /* not link list ST partition */
  499.         return (0);
  500.     }
  501. }
  502.  
  503. countpart(dev)
  504. int dev;
  505. {
  506.     char bs[512];
  507.     PART *ipart;
  508.     int ret, count, i; 
  509.     long sect, start;
  510.  
  511.     if ((ret = getroot(dev, bs, (SECTOR)0)) != 0)    {
  512.         if (tsterr(ret) != OK)
  513.             err(rootread);
  514.         return ERROR;
  515.     }
  516.     ipart = &((RSECT *)(bs + 0x200 - sizeof(RSECT)))->hd_p[0];
  517.     ext = NO_EXT;
  518.     for (i=0; i < NPARTS; i++, ipart++)    {
  519.         if ((ipart->p_flg & P_EXISTS) && 
  520.                 (ipart->p_id[0] == 'X') && 
  521.                 (ipart->p_id[1] == 'G') && 
  522.                 (ipart->p_id[2] == 'M'))    {
  523.             ext = i;
  524.             start = ipart->p_st;
  525.         }
  526.     }
  527.     count = 4;
  528.     if (ext != NO_EXT)    {
  529.         sect = start;
  530.         do    {
  531.             if ((ret = getroot(dev, bs, sect)) != 0)    {
  532.                 if (tsterr(ret) != OK)
  533.                     err(rootread);
  534.                 return ERROR;
  535.             }
  536.                ipart = &((RSECT *)(bs + 0x200 - sizeof(RSECT)))->hd_p[0];
  537.             count++;
  538.             ipart++;                
  539.             sect = ipart->p_st + start;
  540.         } while ((ipart->p_id[0] == 'X') && (ipart->p_id[1] == 'G') &&
  541.                 (ipart->p_id[2] == 'M'));  /* more partition */
  542.     } 
  543.     return (count);
  544. }
  545.